<template>
    <el-dialog
        :before-close="resetForm"
        :close-on-click-modal="false"
        :visible.sync="visible"
        append-to-body
        title="新增系统配置"
        width="540px"
    >
        <div class="edit_page_div">
            <el-form ref="form" :model="form" :rules="rules" :show-message="false" label-width="100px">
                <el-form-item label="配置key" prop="configCode">
                    <el-input v-model="form.configCode" style="width: 400px;"></el-input>
                </el-form-item>
                <el-form-item label="配置名称" prop="configName">
                    <el-input v-model="form.configName" style="width: 400px;"></el-input>
                </el-form-item>
                <el-form-item label="取值类型" prop="inputWay">
                    <select-option v-model="form.inputWay" :list="inputWayList" :select-width="'400px'"></select-option>
                </el-form-item>
                <el-form-item v-if="form.inputWay=='inputWay_select'" :required="form.inputWay=='inputWay_select'" label="取值范围" prop="valueRange">
                    <el-input v-model="form.valueRange" style="width: 400px;"></el-input>
                </el-form-item>
                <el-form-item label="是否启用" prop="enableFlag">
                    <radios-option v-model="form.enableFlag"></radios-option>
                </el-form-item>
            </el-form>
            <div class="edit_button">
                <el-button v-loading.fullscreen.lock="loading" type="primary" @click="submit">保 存</el-button>
                <el-button type="primary" @click="resetForm">取 消</el-button>
            </div>
        </div>
    </el-dialog>
</template>

<script>
import SelectOption from "@/components/select/selectOption";
import RadiosOption from "@/components/radio/radiosOption";

export default {
    name: "constantInsert",
    components: {RadiosOption, SelectOption},
    props: {
        visible: Boolean,
        id: String,
    },
    data() {
        return {
            loading: false,
            form: {
                configKey: '',
                configName: '',
                inputWay: '',
                valueRange: '',
                enableFlag: 1
            },
            rules: {
                configKey: [{required: true, message: '', trigger: 'change'}],
                configName: [{required: true, message: '', trigger: 'change'}],
                inputWay: [{required: true, message: '', trigger: 'change'}],
                enableFlag: [{required: true, message: '', trigger: 'change'}],
            },
            inputWayList: [],
        }
    },
    watch: {
        visible(newVal) {
            this.inputWayList = this.$common.getDicList("inputWay");
        }
    },
    methods: {
        submit() {
            this.$refs.form.validate((valid) => {
                if (valid) {
                    this.loading = true;
                    this.$axios.post("/CHILD_SYSTEM/baseConstant/insert", this.form).then(res => {
                        this.resetForm();
                        this.$message.success(res.message);
                    }).finally(() => {
                        this.loading = false;
                    })
                } else {
                    this.$message.error('表单填写有误');
                    return false;
                }
            });
        },
        resetForm() {
            this.$refs.form.resetFields();
            this.form = this.$options.data().form;
            this.$emit('update:visible', false);
            this.$emit('refreshList');
        }
    }
}
</script>

<style scoped>

</style>